// pages/MovieIndex/movie/movie.js
const MovieHttp = require("../../../utils/MovieHttp");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        hotList: [],
        newList: [],
        topList: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {
        //正在热映
        var hotRes = await MovieHttp.getInTheaters();
        this.handleHotHttp(hotRes);
        //即将上映
        var newRes = await MovieHttp.getNew();
        this.handleNewHttp(newRes);
        //top250
        var topRes = await MovieHttp.getTop250();
        this.handleTopHttp(topRes);
    },
    //评分
    getRating(num) {
        var arr = [];
        for (var i = 0; i < 5; i++) {
            if (num >= 2) {
                arr.push(2);
            } else if (num > 0) {
                arr.push(Math.round(num * 10) / 10)
            } else {
                arr.push(0)
            }
            num = num - 2;
        }
        return arr;
    },

    handleTopHttp(res) {
        res.data.res.forEach(item => {
            item.rat = this.getRating(item.rating);
        })
        this.setData({
            topList: res.data.res.slice(0, 3)
        })
    },
    handleNewHttp(res) {
        res.data.res.forEach(item => {
            item.rat = this.getRating(item.rating);
        })
        this.setData({
            newList: res.data.res.slice(0, 3)
        })
    },
    handleHotHttp(res) {
        res.data.res.forEach(item => {
            item.rating = item.raiting;
            item.rat = this.getRating(item.raiting);
        })
        this.setData({
            hotList: res.data.res.slice(0, 3)
        })
    },
    jumpUrl(url){
        wx.navigateTo({
            url: `/pages/MovieIndex/movieDetail/movieDetail?url=${url}`
        })
    },
    jumpHot(){
        var url = `:4000/api/movie/inTheaters`
        this.jumpUrl(url)
        wx.setNavigationBarTitle({
            title: '正在热映',
        })
    },
    jumpNew(){
        var url = `:4000/api/movie/comingSoon`
        this.jumpUrl(url)
        wx.setNavigationBarTitle({
            title: '即将上映',
        })
    },
    jumpTop(){
        var url = `:4000/api/movie/top250`
        this.jumpUrl(url)
        wx.setNavigationBarTitle({
            title: 'top250',
        })
    },
    clickIpt(){
        wx.navigateTo({
          url: '/pages/MovieIndex/movieSearch/movieSearch',
        })
    }
})